Syntax of Pseudo-Elements in CSS
A pseudo-element in CSS is used to style a specific part of an element or insert content before or after it. The standard syntax uses double colons :: followed by the pseudo-element name.
Syntax format: selector::pseudo-element { properties }
Common pseudo-elements include ::before, ::after, ::first-letter, and ::first-line.
Single colon : notation (e.g., :before) is also supported for backward compatibility with older browsers.
Pseudo-elements do not exist in the DOM; they are virtual elements used for styling purposes.
In this example, ::first-letter targets and styles the first letter of the paragraph, while ::before inserts a star before the paragraph text.
Use pseudo-elements to style or decorate content without modifying the HTML structure.
Combine with CSS properties like content, color, font-size, and background.
Prefer double colon :: notation for clarity and modern standards.
Test pseudo-elements across browsers to ensure consistent rendering.
How would you add a small star icon after every .review-rating element using CSS pseudo-elements?
What happens if you write :before instead of ::before in modern CSS — will it still work?
You’re trying to style a ::after element on a button, but nothing shows up. What’s the first thing you check?
A designer wants a speech bubble tooltip using ::before and ::after, but the triangle part is misaligned on mobile — how do you debug and fix it?
We’re using ::before to add decorative icons in a list, but now the screen reader is announcing them. How do you fix accessibility without removing the visual effect?
Our CSS file has a ::before rule that’s being overridden by another rule — how do you trace the specificity conflict without breaking other styles?
You’re building a reusable card component that uses ::before for a border accent, but it breaks when the card is used inside a scrollable container with overflow hidden — how do you redesign this without losing the visual effect?
In a high-traffic component library, we’re using pseudo-elements for icons to avoid extra DOM nodes. What performance or rendering tradeoffs should you consider at scale?
A legacy UI uses ::after to inject text labels dynamically — now we’re migrating to a React component system. How do you decide whether to keep the pseudo-element or move it to JSX?
We’re standardizing a design system across 12 teams, and some are using pseudo-elements for critical UI cues while others use real elements — how do you enforce consistency without breaking accessibility or performance?
A legacy product relies heavily on ::before and ::after for layout and spacing. We want to migrate to CSS Grid, but removing these breaks dozens of pages. What’s your migration strategy?
How would you architect a CSS architecture that minimizes reliance on pseudo-elements for semantic content while still allowing designers to use them for decoration — and how do you communicate that boundary across engineering and design teams?